#include "myScreen.h"

MyScreen::MyScreen()
{
}

MyScreen::~MyScreen()
{
}

void MyScreen::Render()
{
	this->pEngine->GetGraphics()->DrawTexture(this->pMyBackground, 0, 0, this->ScreenWidth(), this->ScreenHeight());

	this->pEngine->GetGraphics()->SetGlobalColor(55, 55, 55);
	this->pEngine->GetGraphics()->DrawRect((this->ScreenWidth() / 2.0f) - 100.0f, (this->ScreenHeight() / 2.0f) - 12.0f, 200.0f, 24.0f);
	this->pEngine->GetGraphics()->SetGlobalColor(255, 255, 255);
	this->pEngine->GetGraphics()->DrawRect((this->ScreenWidth() / 2.0f) - 100.0f, (this->ScreenHeight() / 2.0f) - 12.0f, this->pMyMusic->GetPosition() * 200.0f, 24.0f);

	this->pEngine->GetGraphics()->DrawText("Instruction", 10, 10 + (18 * 0), this->pMyFont);
	this->pEngine->GetGraphics()->DrawText("Press Q to pause or resume the music", 10, 10 + (18 * 2), this->pMyFont);
	this->pEngine->GetGraphics()->DrawText("Press W to replay the music", 10, 10 + (18 * 3), this->pMyFont);
	this->pEngine->GetGraphics()->DrawText("Press E to play the sound effect", 10, 10 + (18 * 4), this->pMyFont);

	string sText = "Music progress";
	int iTextWidth = this->pEngine->GetGraphics()->TextWidth(sText, this->pMyFont);
	this->pEngine->GetGraphics()->DrawText(sText.c_str(), (this->ScreenWidth() / 2.0f) - (iTextWidth / 2.0f), (this->ScreenHeight() / 2.0f) - 50.0f, this->pMyFont);
}

void MyScreen::Update(float fDelta)
{
	if (this->pEngine->GetWindowHandle()->HasFocus() == false)
		this->pMyMusic->SetVolume(0);
	else
	{
		this->pMyMusic->SetVolume(0.6f);

		if (this->pEngine->GetInput()->IsKeyPressed(DelGE_Key_E))
			this->pMySound->Play();

		if (this->pEngine->GetInput()->IsKeyPressed(DelGE_Key_W))
			this->pMyMusic->Play();

		if (this->pEngine->GetInput()->IsKeyPressed(DelGE_Key_Q))
		{
			if (!this->pMyMusic->IsPaused()) 
				this->pMyMusic->Pause();
			else
				this->pMyMusic->Resume();
		}
	}
}

void MyScreen::Load()
{
	this->pMyBackground = this->pEngine->GetGraphics()->LoadTexture("media/bkgd11.png", Color(255, 0, 0));

	this->pMyFont = this->pEngine->GetGraphics()->LoadFont("media/8bitlim.ttf", 14);

	this->pMyMusic = this->pEngine->GetAudio()->LoadMusic("media/music.mid");
	this->pMyMusic->SetVolume(0.6f);
	this->pMyMusic->Play();

	this->pMySound = this->pEngine->GetAudio()->LoadSound("media/horn.wav");
	this->pMySound->SetVolume(1.0f);
}

void MyScreen::Unload()
{
	this->pEngine->GetGraphics()->UnloadTexture(this->pMyBackground);
	this->pEngine->GetGraphics()->UnloadFont(this->pMyFont);

	this->pEngine->GetAudio()->UnloadMusic(this->pMyMusic);
	this->pEngine->GetAudio()->UnloadSound(this->pMySound);
}